<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //表名
    protected $table = 'article';

    //主键
    protected $primaryKey = 'article_id';

    //时间戳
    public $timestamps = false;

    //获取列表
    public static function get_article_list($count = 15){
    	$article_list = self::take($count)->orderBy('article_id', 'desc')->get();
		foreach ($article_list as $key=>$article) {
		    $article_list[$key]->add_time = date('Y-m-d H:i:s', $article->add_time);
		}
    	return $article_list;
    }

    //获取信息
    public static function get_article_info($id){
        $article_info = self::find($id);
        $article_info['add_time'] = date('Y-m-d H:i:s', $article_info->add_time);
        //点赞状态
        $session_id = session()->getId();
        $article_praise = new ArticlePraise;
        $praise_status = $article_praise->check_praise_status($id, $session_id);
        if(empty($praise_status)){
            $article_info->praise_status = 0;
        }else{
            $article_info->praise_status = 1;
        }
        return $article_info;
    }

    //获取最新文章
    public static function get_newest_article_info(){
        $article_info = self::orderBy('article_id', 'desc')->first();
        $article_info['add_time'] = date('Y-m-d H:i:s', $article_info->add_time);
        return $article_info;
    }
}
